package com.controller;/**
 * @AUTHOR: 我是章璐锋憨憨
 * @Data:
 * @Version:
 */

import com.pojo.Provider;
import com.service.ProviderService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Objects;

/**
 * @outhor RD
 * 2021/12/24
 */
@WebServlet("/provider/*")
public class ProviderServlet extends BaseServlet {
    private ProviderService service = new ProviderService();
    /**当前页数*/
    private Integer currentPageNo = 1;
    private Integer pageSize = 8;

    public void providerList(HttpServletRequest req, HttpServletResponse resq) throws IOException, ServletException {
        List<Provider> list = service.getProviders();
        //每页可容纳数据数量
        List<Provider> listThis = service.getProviders(currentPageNo, pageSize);
        req.getSession().setAttribute("SERVLET_NOW","provider");
        req.getSession().setAttribute("PROVIDER_LIST", listThis);
        req.getSession().setAttribute("currentPageNo", currentPageNo);
        req.getSession().setAttribute("totalCount", list.size());
        if (list.size()!=0){
            req.getSession().setAttribute("totalPageCount", ((list.size()-1)/ pageSize)+1);
        }else {
            req.getSession().setAttribute("totalPageCount", 0 );
        }
        req.setAttribute("KEY_WORD", null);
//        resq.sendRedirect(req.getContextPath()+"/jsp/providerlist.jsp");
        req.getRequestDispatcher("/jsp/providerlist.jsp").forward(req, resq);
    }

    public void providerSearch(HttpServletRequest req, HttpServletResponse resq) throws ServletException, IOException {
        String queryProCode = req.getParameter("queryProCode");
        String queryProName = req.getParameter("queryProName");
        Provider p = new Provider();
        p.setProCode(queryProCode);
        p.setProName(queryProName);
        List<Provider> list = service.search(queryProCode, queryProName);
        req.getSession().setAttribute("PROVIDER_LIST", list);
//        req.getSession().setAttribute("param.totalCount",list.size());
        req.setAttribute("KEY_WORD", p);
//        req.getRequestDispatcher("/jsp/providerlist.jsp").forward(req, resq);
        resq.sendRedirect(req.getContextPath() + "/jsp/providerlist.jsp");
    }

    public void providerSave(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        String proDesc = req.getParameter("proDesc");
        Provider provider = new Provider(proCode, proName, proContact, proPhone, proAddress, proFax, proDesc);
        if (!service.save(provider)) {
            req.getRequestDispatcher("/jsp/error.jsp").forward(req, resp);
        }
//        req.getRequestDispatcher("/provider/providerList").forward(req,resp);
        resp.sendRedirect("providerList");
    }

    public void providerView(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        Provider provider = service.get(id);
        if (!Objects.nonNull(provider)) {
            req.getRequestDispatcher("/jsp/error.jsp").forward(req, resp);
        }
        req.getSession().setAttribute("PROVIDER_ONE", provider);
//        resp.sendRedirect(req.getContextPath()+"/jsp/providerview.jsp");
        req.getRequestDispatcher("/jsp/providerview.jsp").forward(req, resp);
    }

    public void providerModify(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        Integer id = Integer.valueOf(req.getParameter("id"));
        Provider provider = service.get(id);
        System.out.println(provider);
        if (!Objects.nonNull(provider)) {
            req.getRequestDispatcher("/jsp/error.jsp").forward(req, resp);
        }
        req.getSession().setAttribute("PROVIDER_EDIT", provider);
        resp.sendRedirect(req.getContextPath() + "/jsp/providermodify.jsp");
    }

    public void providerEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        Provider provider = (Provider) req.getSession().getAttribute("PROVIDER_EDIT");
        Integer id = provider.getId();
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        String proDesc = req.getParameter("proDesc");
        Provider pro = new Provider(proCode, proName, proContact, proPhone, proAddress, proFax, proDesc);
        if (!service.modify(id, pro)) {
            resp.sendRedirect(req.getContextPath() + "/jsp/error.jsp");
        }
//        req.getRequestDispatcher("/provider/providerList").forward(req,resp);
        resp.sendRedirect("providerList");
    }

    public void providerDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        service.remove(id);
//        req.getRequestDispatcher("providerList").forward(req,resp);
        resp.sendRedirect("provider/providerList");
    }

    public void pageJumpTo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        currentPageNo = Integer.valueOf(req.getParameter("inputPage"));
        resp.sendRedirect("providerList");
    }

    public void pageFirst(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        currentPageNo = 1;
        resp.sendRedirect("providerList");
    }

    public void pageBefore(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        if (this.currentPageNo>1){
            this.currentPageNo -= 1;
        }else {
            currentPageNo = 1;
        }
        resp.sendRedirect("providerList");
    }

    public void pageNext(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer totalPageCount = (Integer) req.getSession().getAttribute("totalPageCount");
        if (totalPageCount > currentPageNo){
            currentPageNo += 1;
        }else {
            currentPageNo = totalPageCount;
        }
        resp.sendRedirect("providerList");
    }

    public void pageFinal(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        currentPageNo = (Integer) req.getSession().getAttribute("totalPageCount");
        resp.sendRedirect("providerList");
    }
}



